package cn.zmwh.portal.modules.index.controller;


import cn.zmwh.common.core.api.Result;
import cn.zmwh.common.core.constants.Constants;
import cn.zmwh.portal.modules.index.pojo.entity.TlUserCircleFollow;
import cn.zmwh.portal.modules.index.service.TlUserCircleFollowService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;

/**
 * <p>
 * 关注圈子 前端控制器
 * </p>
 *
 * @author dmzmwh
 * @since 2024-10-02
 */
@RestController
@RequestMapping("/api/circle")
public class TlUserCircleFollowController {


    @Autowired
    TlUserCircleFollowService tlUserCircleFollowService;

    @PostMapping("/follow/{id}/{type}")
    @ApiOperation(value = "关注")
    public Result follow(@ApiIgnore @RequestHeader(Constants.UID) Long tlId,
                         @PathVariable Long id,
                         @PathVariable int type) {

        LambdaQueryWrapper<TlUserCircleFollow> eq = Wrappers.<TlUserCircleFollow>lambdaQuery()
                .eq(TlUserCircleFollow::getUid, tlId)
                .eq(TlUserCircleFollow::getCircleId, id);
        if (type == 1) {
            tlUserCircleFollowService.remove(eq);
        } else {
            TlUserCircleFollow one = tlUserCircleFollowService.getOne(eq);
            if (one == null) {
                one = new TlUserCircleFollow();
                one.setCircleId(id);
                one.setUid(tlId);
                one.setCreateTime(new Date());
                tlUserCircleFollowService.saveOrUpdate(one);
            } else {
                return Result.success();
            }
        }

        return Result.success();
    }
}

